//
//  MyOrderTableViewCell.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/10.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

protocol MyOrderTableViewCellDelegate: NSObjectProtocol {
    func someButtonTap(cell: MyOrderTableViewCell, title: String)
}

class MyOrderTableViewCell: UITableViewCell {
    weak var delegate: MyOrderTableViewCellDelegate?
    @IBOutlet weak var typeLabel: UILabel!
    @IBOutlet weak var leftButton: UIButton!
    @IBOutlet weak var rightButton: UIButton!
    
    
    @IBOutlet weak var orderNumber: UILabel!
    
    @IBOutlet weak var productName: UILabel!
    
    @IBOutlet weak var productModel: UILabel!
    
    @IBOutlet weak var numberLable: UILabel!
    
    @IBOutlet weak var headerImage: UIImageView!
    @IBOutlet weak var priceLable: UILabel!
    var type: OrderDetailType = .waitPay
    
    var data: purcharseModel?
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
//        self.clipsToBounds = true
        // Initialization code
    }
    
    func setUI () {
//        leftButton.layer.cornerRadius = leftButton.height / 2
//        leftButton.layer.borderWidth = 1
//        rightButton.layer.cornerRadius = rightButton.height / 2
//        rightButton.layer.borderWidth = 1
    }
    
    func bindData(data: purcharseModel) -> Void {
        
        self.data = data
        let perchas = data
//        orderNumber.text = data.orderNumber
        productName.text = perchas.goodsName
        productModel.text = perchas.specAndCol
        headerImage.sd_setImage(with: appendUrl(suffix: (perchas.imageUrl)!), completed: nil)
        priceLable.text = "￥：" + (perchas.totalPrice)!
        numberLable.text = "x " + perchas.number!
    }
    func setData(type: OrderDetailType) {
        self.type = type
//        typeUI()
    }
    
    
    //0:待付款；1：待出库；2：已出库；3：已完成；4：已取消；5：申请退换；6：同意退货；7：拒绝退货
    private func typeUI() {
        var leftButtonIsHidden = true
        var rightButtonColor = UIColor(hex: 0x8F8F8F)
        var typeTitle = ""
        var typeTitleColor = ONColor.currencyRedColor
        switch type {
        case .waitPay:
            typeTitle = "待付款"
            rightButtonColor = ONColor.currencyRedColor
            rightButton.setTitle("立即付款", for: .normal)
        case .waitOut:
            typeTitle = "待出库"
            rightButtonColor = ONColor.currencyRedColor
            rightButton.setTitle("催单", for: .normal)
        case .out:
            typeTitle = "已出库"
            rightButtonColor = ONColor.currencyRedColor
            leftButtonIsHidden = false
            rightButton.setTitle("确认收货", for: .normal)
        case .done:
            typeTitle = "已完成"
            rightButtonColor = ONColor.currencyRedColor
            rightButton.setTitle("再次购买", for: .normal)
        case .cancel:
            typeTitle = "已取消"
            rightButtonColor = ONColor.currencyRedColor
            rightButton.setTitle("立即付款", for: .normal)
        case .req:
            typeTitle = "申请退换"
            leftButtonIsHidden = false
            rightButtonColor = ONColor.currencyRedColor
            rightButton.setTitle("进行评价", for: .normal)
        case .agree:
            typeTitle = "同意退货"
            rightButton.setTitle("再次购买", for: .normal)
        case .none:
            typeTitle = "拒绝退货"
            rightButton.setTitle("再次购买", for: .normal)
        }
        
        typeLabel.textColor = typeTitleColor
        typeLabel.text = typeTitle
        leftButton.isHidden = leftButtonIsHidden
        rightButton.layer.borderColor = rightButtonColor.cgColor
        rightButton.setTitleColor(rightButtonColor, for: .normal)
    }
    
    @IBAction func commentTap(_ sender: UIButton) {
        guard let currentTitle = sender.currentTitle else { return  assert(false, "按钮Title没有设置")}
        delegate?.someButtonTap(cell: self, title: currentTitle)
    }
   
}
